﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.客户管理;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.Export;
using SmartProduction.Read.API.Dtos;
using System.Linq;

namespace SmartProduction.Read.API.Application.CommandHandlers.ExportHandler
{
    public class ExportSalesOrderCommandHandler : IRequestHandler<ExportSalesOrderCommand, APIResult<List<ExportSalesOrderCommandityDTO>>>
    {
        /// <summary>
        /// 销售订单仓储
        /// </summary>
        private readonly IBaseRepository<SalesOrder> salesorderRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="salesorderRepository">销售订单仓储</param>
        /// <param name="mapper">映射</param>
        public ExportSalesOrderCommandHandler(IBaseRepository<SalesOrder> salesorderRepository,IMapper mapper)
        {
            this.salesorderRepository = salesorderRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回</returns>
        public Task<APIResult<List<ExportSalesOrderCommandityDTO>>> Handle(ExportSalesOrderCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<ExportSalesOrderCommandityDTO>> result = new APIResult<List<ExportSalesOrderCommandityDTO>>();

                var list = from a in salesorderRepository.GetAll().Result
                           where request.SalesOrderId.Contains(a.SalesOrderId)
                           select new ExportSalesOrderCommandityDTO
                           {
                               SalesOrderNumer = a.SalesOrderNumer,
                               AltEnter = a.AltEnter,
                               ClientInfoNumer = a.ClientInfoNumer,
                               AmountPrice = a.AmountPrice,
                               SalesOrderPrice = a.SalesOrderPrice,
                               SalesOrderType = a.SalesOrderType,
                               Taxrate = a.Taxrate,
                               TaxType = a.TaxType,
                               Currency = a.Currency,
                               InvoicePrice = a.InvoicePrice,
                               SalesMan = a.SalesMan,
                               SalesDepart = a.SalesDepart,
                           };

                result.Code = APIEnums.查询成功;
                result.Msg = APIEnums.查询成功.ToString();
                result.data = list.ToList();

                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
